package com.castile.secondkill.controller;

import com.castile.secondkill.controller.vo.ItemVO;
import com.castile.secondkill.exception.BusinessException;
import com.castile.secondkill.response.CommonResponse;
import com.castile.secondkill.service.ItemService;
import com.castile.secondkill.service.model.ItemModel;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品
 *
 * @author Hongliang Zhu
 * @create 2023-03-18 21:16
 */
@Controller
@RequestMapping("/item")
public class ItemController extends BaseController {

    @Autowired
    private ItemService itemService;

    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResponse createItem(@RequestParam(name = "title") String title,
                                     @RequestParam(name = "description") String description,
                                     @RequestParam(name = "price") BigDecimal price,
                                     @RequestParam(name = "stock") Integer stock,
                                     @RequestParam(name = "imgUrl") String imgUrl) throws BusinessException {

        // 封装service请求用来创建商品
        ItemModel itemModel = new ItemModel();
        itemModel.setTitle(title);
        itemModel.setPrice(price);
        itemModel.setDescription(description);
        itemModel.setStock(stock);
        itemModel.setImgUrl(imgUrl);
        ItemModel item = itemService.createItem(itemModel);
        // 返回给前端的是ItemVO
        ItemVO itemVO = convertVOFromModel(item);
        return CommonResponse.create(itemVO);
    }

    /**
     * 商品详情页的浏览
     */
    @RequestMapping(value = "/get", method = {RequestMethod.GET})
    @ResponseBody
    public CommonResponse getItem(@RequestParam(name = "id") Integer id) {
        ItemModel item = itemService.getItem(id);
        ItemVO itemVO = convertVOFromModel(item);
        return CommonResponse.create(itemVO);
    }

    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    @ResponseBody
    public CommonResponse listItem() {
        List<ItemModel> itemModels = itemService.listItem();
        List<ItemVO> collect = itemModels.stream().map(itemModel -> {
            ItemVO itemVO = this.convertVOFromModel(itemModel);
            return itemVO;
        }).collect(Collectors.toList());

        return CommonResponse.create(collect);
    }

    private ItemVO convertVOFromModel(ItemModel itemModel) {
        if (itemModel == null) {
            return null;
        }
        ItemVO itemVO = new ItemVO();
        BeanUtils.copyProperties(itemModel, itemVO);
        if (itemModel.getPromoModel() != null) {
            // 有正在进行的秒杀活动
            itemVO.setPromoStatus(itemModel.getPromoModel().getStatus());
            itemVO.setPromoId(itemModel.getPromoModel().getId());
            itemVO.setPromoPrice(itemModel.getPromoModel().getPromoItemPrice());
            itemVO.setStartDate(itemModel.getPromoModel().getStartDate().toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")));
        } else {
            itemVO.setPromoStatus(0);
        }

        return itemVO;
    }

}
